package com.dfbz.dao;

import com.dfbz.entity.Meeting;
import org.springframework.dao.DataAccessException;
import org.springframework.jdbc.core.BeanPropertyRowMapper;

import java.util.List;

public class MeetingDao extends baseDao {
    /**
     * 分页+查询
     *
     * @param title
     * @param status
     * @param startIndex
     * @param pageSize
     * @return
     */
    public List<Meeting> findByTileAndStatus(String title, Integer status, Integer startIndex, Integer pageSize) {
        String sql = "";
        Object[] args = null;
        if (status == -1) {

            // 代表前端没有勾选status参数
            sql = "select * from meeting where title like ? order by publish_date limit ?,?";
            args = new Object[]{"%" + title + "%", startIndex, pageSize};
        } else {
            // 勾选了status参数
            sql = "select * from meeting where title like ? order by publish_date and status = ? limit ?,?";
            args = new Object[]{"%" + title + "%", status, startIndex, pageSize};
        }
        return jdbcTemplate.query(sql,new BeanPropertyRowMapper<>(Meeting.class),args);
    }

    public Integer countByTitleAndStatus(String title, Integer status) {
        String sql = "";
        Object[] args = null;
        if (status == -1) {
            //代表前端没有勾选
            sql = "select count(1) from meeting where title like? ";
            args = new Object[]{"%" + title + "%"};
        } else {
            sql = "select count(1) from meeting where title like? and status=? ";
            args = new Object[]{"%" + title + "%", status};
        }
        return jdbcTemplate.queryForObject(sql, Integer.class, args);
    }

    public void save(Meeting meeting) {
        jdbcTemplate.update("insert into meeting values(null,?,?,?,?,?,?,?,?,?)",
                meeting.getDeptName(),
                meeting.getDeptId(),
                meeting.getTitle(),
                meeting.getContent(),
                meeting.getPublishDate(),
                meeting.getStartTime(),
                meeting.getEndTime(),
                meeting.getStatus(),
                "["+meeting.getMakeUser()+"]"
        );
    }

    public Meeting findByid(long id) {
        try {
            return jdbcTemplate.queryForObject("SELECT * from meeting where id=?",new BeanPropertyRowMapper<>(Meeting.class),id);
        } catch (DataAccessException e) {
            return null;
        }
    }

    public Integer countJoinMeetingByMeetingId(long id) {
        try {
            return  jdbcTemplate.queryForObject("SELECT count(1) from meeting_join where c_id=?",Integer.class,id);
        } catch (DataAccessException e) {
            return null;
        }
    }

    public Integer isJoinMeeting(Long loginUserId, long id) {
        try {
            return jdbcTemplate.queryForObject("SELECT count(1) from meeting_join where u_id=? and c_id=?",Integer.class,loginUserId,id);
        }catch (Exception e){
            return null;
        }
    }

    public void unJoin(Long loginUserId, long id) {
        jdbcTemplate.update("delete from meeting_join where u_id=? and c_id=?", loginUserId, id);
    }

    public void CanJoin(Long loginUserId, long id) {
        jdbcTemplate.update("insert into meeting_join values(?,?)", loginUserId, id);
    }
}
